limsControlmaterial.editPage = {

    getGridParam: function (activeStatus) {
        var _this = this;
        var grid = {
            type: 'grid',
            id: 'lims-controlmaterial-edit-list-grid',
            url: limsControlmaterial.baseUrl + '/queries/raw',
            service: 'limsControlmaterialServiceImpl',
            dbTable: 'T_LIMS_CONTROLMATERIAL',
            columns: limsControlmaterial.getEditListColumns(),
            page: true,
            onCellClick: function (field, row) {
                // field === 'controlMaterialName' && _this.loadDetailPage(row.id);
            },
            requestData: {
                activeStatus_EQ: 'on'
            },
            deleteFormatter: function (row) {
                return row.controlMaterialName
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASELINE.BASEDATA.CONTROLMATERIALS.MODAL.ADD',
                        fields: limsControlmaterial.getBaseInfoFormFields(),
                        url: limsControlmaterial.baseUrl,
                        disableNotEmptyValidate : true,
                        titleWidth: 120
                    }).done(function (id) {
                        _this.refreshGrids();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    var selectRows = Gikam.getComp('lims-controlmaterial-edit-list-grid').getSelections()
                    if (Gikam.isEmpty(selectRows)) {
                        return
                    }
                    Gikam.postText(limsControlmaterial.baseUrl + '/check-ifstill-inuse', Gikam.getJsonWrapper(null, [null, selectRows])).done(function (req) {
                        req = JSON.parse(req)
                        if (req.flag) {
                            Gikam.alert(Gikam.propI18N('QUALITY_CONTROL.PLANPROJECT.CANNOT_DELETE') + '<br/>' + req.message)
                        } else {
                            Gikam.getComp('lims-controlmaterial-edit-list-grid').deleteRows(limsControlmaterial.baseUrl)
                        }
                    });
                }
            }]
        }
        if (activeStatus) {
            switch (activeStatus) {
                case 'on':
                    grid.toolbar.push({
                        type: 'button',
                        text: 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.OPTIONS.OFF',
                        icon: 'disable',
                        onClick: function () {
                            var grid_ = Gikam.getComp('lims-controlmaterial-edit-list-grid')
                            var setectRows = grid_.getSelections()
                            if (setectRows.length > 0) {
                                Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.TIPS.OFF', function () {
                                    var ids = setectRows.map(function (item) {
                                        return {id: item.id}
                                    })
                                    Gikam.postText(limsControlmaterial.baseUrl + '/activeoff', Gikam.getJsonWrapper(null, ['limsControlmaterialServiceImpl', ids])).done(function (data) {
                                        _this.refreshGrids();
                                    });
                                })
                            }
                        }
                    })
                    break
                case 'off':
                    grid.toolbar = [{
                        type: 'button',
                        text: 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.OPTIONS.ON',
                        icon: 'enable',
                        onClick: function () {
                            var grid_ = Gikam.getComp('lims-controlmaterial-edit-list-grid-off')
                            var setectRows = grid_.getSelections()
                            if (setectRows.length > 0) {
                                Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.TIPS.ON', function () {
                                    var ids = setectRows.map(function (item) {
                                        return {id: item.id}
                                    })
                                    Gikam.postText(limsControlmaterial.baseUrl + '/activeon', Gikam.getJsonWrapper(null, ['limsControlmaterialServiceImpl', ids])).done(function (data) {
                                        _this.refreshGrids();
                                    });
                                })
                            }
                        }
                    }]
                    grid.id = 'lims-controlmaterial-edit-list-grid-off'
                    grid.requestData = {
                        activeStatus_EQ: 'off'
                    }
                    break
            }
        }
        return grid
    },

    refreshGrids() {
        Gikam.getComp('lims-controlmaterial-edit-list-grid').refresh()
        Gikam.getComp('lims-controlmaterial-edit-list-grid-off').refresh()
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsControlmaterial.baseUrl + '/page/detail' + Gikam.param({
            id: id
        }));
    },

    getMainTabs() {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.ON',
                items: [_this.getGridParam('on')]
            }, {
                title: 'T_LIMS_CONTROLMATERIAL.ACTIVESTATUS.OFF',
                items: [_this.getGridParam('off')]
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-controlmaterial-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getMainTabs()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
